package net.bbtang.website.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.bbtang.website.mapper.GroupMapper;
import net.bbtang.website.mapper.GroupMealMapper;
import net.bbtang.website.mapper.GroupRefMapper;
import net.bbtang.website.mapper.UserMapper;
import net.bbtang.website.pojo.UserGroupDTO;
import net.bbtang.website.pojo.UserGroupMealDTO;
import net.bbtang.website.pojo.UserGroupRefDTO;
import net.bbtang.website.service.GroupApplyInfoService;
import net.bbtang.website.service.GroupInfoService;
import net.bbtang.website.service.GroupRefInfoService;
import net.bbtang.website.utils.GroupConstantsStatus;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;




@Service("groupRefInfoService")
public class GroupRefInfoServiceImpl implements GroupRefInfoService {
	
	private static final Logger logger = Logger.getLogger(GroupRefInfoServiceImpl.class);
	
	@Autowired
	GroupRefMapper groupRefMapper;
	@Autowired
	UserMapper userMapper;

	@Override
	public void applyJoinGroup(Integer userId, Integer groupId) {
		UserGroupRefDTO groupRef = new UserGroupRefDTO();
		groupRef.setUserId(userId);
		groupRef.setAdminType(GroupConstantsStatus.GROUP_MEMBER);
		groupRef.setApplyState(GroupConstantsStatus.APPLY_JOIN);
		groupRef.setGroupId(groupId);
		groupRefMapper.applyJoinGroup(groupRef);
		
	}

	@Override
	public void confirmJoinGroup(Integer userId, Integer groupId) {
		UserGroupRefDTO groupRef = new UserGroupRefDTO();
		groupRef.setUserId(userId);
		groupRef.setApplyState(GroupConstantsStatus.CONFIRM_JOIN);
		groupRef.setGroupId(groupId);
		groupRefMapper.confirmJoinGroup(groupRef);
	}

	@Override
	public List<UserGroupRefDTO> getGroupMember(Integer groupId,Integer applyState) {
		Map<String,Object> map =new HashMap<String, Object>();
		map.put("groupId", groupId);
		map.put("applyState", applyState);
		List<UserGroupRefDTO> groupRefList = groupRefMapper.getGroupMember(map);
		if(groupRefList != null && groupRefList.size() > 0){
			for (UserGroupRefDTO userGroupRefDTO : groupRefList) {
				Map<String,Object> userMap = new HashMap<String, Object>();
				userMap.put("userId", userGroupRefDTO.getUserId());
				userGroupRefDTO.setUser(userMapper.getUserInfoById(userMap));
			}
		}
	
		
		return groupRefList;
	}

	@Override
	public UserGroupRefDTO getGroupRefById(Integer userId, Integer groupId) {
		return groupRefMapper.getGroupRefById(userId,groupId);
	}

	

	


}
